package com.crr.controller;

import com.crr.entity.Result;
import com.crr.entity.User;
import com.crr.service.UserService;
import com.github.pagehelper.Page;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/toList")
    public ModelAndView toList() {
        ModelAndView mv = new ModelAndView("list");
        return mv;
    }

    @RequestMapping("/getPage")
    public Map queryUserByPage(int pageNum, int pageSize, User user) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("user", user);
        Page page = userService.queryLikeUsers(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }

    /*   @RequestMapping("/add")
       public Result addUser(User user){
           userService.addUser(user);
           return new Result();
       }*/
    @RequestMapping("/add")
    public Result addUser(User user) {
        userService.addUser(user);
        return new Result();
    }

    @RequestMapping("/del")
    public Result delUser(User user) {
        userService.delUser(user);
        return new Result();
    }

    @RequestMapping("upd")
    public Result updUser(User user) {
        userService.updUser(user);
        return new Result();
    }
@RequestMapping("/editpage")
    public ModelAndView findUser(User user) {
        ModelAndView mv = new ModelAndView("edit");
        User u = userService.getUser(user);
        mv.addObject("data", u);
        return mv;
    }

    /**
     * 登录页面
     * @return
     */
    @RequestMapping("login")
    public ModelAndView loginck(){
        return new ModelAndView("login");
    }
    @RequestMapping("userlogin")
    public ModelAndView login(User user, HttpSession session, Model model){
       User u= userService.login(user);
       if(u!=null){
           session.setAttribute("user",user);
           return new ModelAndView("redirect:/toList") ;
       }else{
           model.addAttribute("msg","登录失败");
           return new ModelAndView("login");
       }
    }
}
